/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.ssau.lib;

import java.io.Serializable;
import java.util.Collection;
import org.jdom.Element;
import ru.ssau.utils.XMLHelper;

/**
 *
 * @author Дима
 */
public class StringIntegerPair implements Serializable, XMLizable {

    private Integer number;
    private String string;

    public Element toXMLElement() {
        Element root = new Element("StringIntegerPair");
        XMLHelper xhelp = new XMLHelper();
        if (number != null) {
            xhelp.addElement(root, "number", number);
        }
        if (string != null) {
            xhelp.addElement(root, "string", string);
        }
        return root;
    }

    public void loadFromXMLElement(Element data) {
        Element temp = null;
        if ((temp = data.getChild("number")) != null) {
            setNumber(Integer.valueOf(temp.getValue()));
        }
        if ((temp = data.getChild("string")) != null) {
            setString(temp.getValue());
        }
    }

    public StringIntegerPair(int number, String string) {
        this.number = number;
        this.string = string;
    }

    public StringIntegerPair() {
    }

    public void setNumber(int number) {
        this.number = Integer.valueOf(number);
    }

    public Integer getNumber() {
        return number;
    }

    public void setString(String string) {
        this.string = string;
    }

    public String getString() {
        return string;
    }

    @Override
    public String toString() {
        return number + ". " + string;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (obj == this) {
            return true;
        }
        if (obj.getClass() != StringIntegerPair.class) {
            return false;
        }
        StringIntegerPair other = (StringIntegerPair) obj;
        return other.getNumber().equals(number) && other.getString().equals(string);
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 43 * hash + (this.number != null ? this.number.hashCode() : 0);
        hash = 43 * hash + (this.string != null ? this.string.hashCode() : 0);
        return hash;
    }

    public static StringIntegerPair[] toStringIntegerPairArray(Collection<? extends PresentableAsStringIntegerPair> c) {
        PresentableAsStringIntegerPair[] values = c.toArray(new PresentableAsStringIntegerPair[0]);
        StringIntegerPair[] pairs = new StringIntegerPair[values.length];
        for (int i = 0; i < values.length; i++) {
            pairs[i] = values[i].toStringIntegerPair();
        }
        return pairs;
    }
}
